package com.youku.plugins.topbarV4.view
{
   import flash.display.MovieClip;
   import com.youku.interfaces.IPlayerProxy;
   import com.youku.events.ConductorEvent;
   import com.youku.events.PlayerControlEvent;
   import com.youku.events.CoreEvent;
   import com.youku.events.PluginEvent;
   import flash.events.MouseEvent;
   import com.youku.plugins.topbarV4.TopbarEvent;
   import com.youku.data.PlayerConstant;
   import com.youku.utils.Tweener;
   import com.youku.conductor.ConductorState;
   
   public class TopbarMediator extends Object
   {
      
      public function TopbarMediator(param1:MovieClip, param2:IPlayerProxy) {
         super();
         this._mainMc = param1;
         this._playerProxy = param2;
         this._topbar = new Topbar();
         this.init();
      }
      
      private var _mainMc:MovieClip;
      
      private var _playerProxy:IPlayerProxy;
      
      private var _topbar:Topbar;
      
      private var _canWheel:Boolean = true;
      
      private function init() : void {
         this._playerProxy.addEventListener(ConductorEvent.STATE_CHANGED,this.onStateChanged);
         this._playerProxy.addEventListener(ConductorEvent.METADATA_UPDATED,this.onMetadata);
         this._playerProxy.addEventListener(PlayerControlEvent.PLAYER_SIZE_CHANGED,this.onPlayerControlEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.FULLSCREEN_CHANGED,this.onPlayerControlEvent);
         this._playerProxy.addEventListener(CoreEvent.SCREEN_MODE_CHANGED,this.onVideoScreenModeChanged);
         this._playerProxy.addEventListener(PluginEvent.CONTROL_BAR_START_HIDE,this.onOtherPluginEvent);
         this._playerProxy.addEventListener(PluginEvent.CONTROL_BAR_SHOW,this.onOtherPluginEvent);
         this._playerProxy.addEventListener(MouseEvent.MOUSE_WHEEL,this.onMouseWheel);
         this._playerProxy.addEventListener(PluginEvent.PROGRAMLIST_SHOW,this.onOtherPluginEvent);
         this._playerProxy.addEventListener(PluginEvent.PROGRAMLIST_HIDE,this.onOtherPluginEvent);
         this.topbar.addEventListener(TopbarEvent.TOPBAR_50_CLICK,this.onTopbarEvent);
         this.topbar.addEventListener(TopbarEvent.TOPBAR_75_CLICK,this.onTopbarEvent);
         this.topbar.addEventListener(TopbarEvent.TOPBAR_100_CLICK,this.onTopbarEvent);
         this.topbar.addEventListener(TopbarEvent.TOPBAR_FULL_CLICK,this.onTopbarEvent);
         this.topbar.addEventListener(TopbarEvent.TOPBAR_CLOSE_CLICK,this.onTopbarEvent);
      }
      
      private function onMouseWheel(param1:MouseEvent) : void {
         if(!this._playerProxy.playerData.isInFullScreen || !this._canWheel)
         {
            return;
         }
         if(param1.delta > 0)
         {
            this.topbar.upgradeMode();
         }
         else if(param1.delta < 0)
         {
            this.topbar.downgradeMode();
         }
         
      }
      
      private function onOtherPluginEvent(param1:PluginEvent) : void {
         switch(param1.type)
         {
            case PluginEvent.CONTROL_BAR_SHOW:
               this.tweenShow();
               break;
            case PluginEvent.CONTROL_BAR_START_HIDE:
               this.tweenHide();
               break;
            case PluginEvent.PROGRAMLIST_SHOW:
               this._canWheel = false;
               break;
            case PluginEvent.PROGRAMLIST_HIDE:
               this._canWheel = true;
               break;
         }
      }
      
      private function onTopbarEvent(param1:TopbarEvent) : void {
         switch(param1.type)
         {
            case TopbarEvent.TOPBAR_50_CLICK:
               this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.TOPBAR_CHANGE_SCREEN_TYPE,PlayerConstant.SCREEN_MODE_50));
               break;
            case TopbarEvent.TOPBAR_75_CLICK:
               this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.TOPBAR_CHANGE_SCREEN_TYPE,PlayerConstant.SCREEN_MODE_75));
               break;
            case TopbarEvent.TOPBAR_100_CLICK:
               this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.TOPBAR_CHANGE_SCREEN_TYPE,PlayerConstant.SCREEN_MODE_100));
               break;
            case TopbarEvent.TOPBAR_FULL_CLICK:
               this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.TOPBAR_CHANGE_SCREEN_TYPE,PlayerConstant.SCREEN_MODE_FULL));
               break;
            case TopbarEvent.TOPBAR_CLOSE_CLICK:
               this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.TOPBAR_CLOSE_FULLSCREEN));
               break;
         }
      }
      
      private function onVideoScreenModeChanged(param1:CoreEvent) : void {
         var _loc2_:String = param1.data.mode;
         this.topbar.setScreenMode(_loc2_);
      }
      
      protected function get topbar() : Topbar {
         return this._topbar as Topbar;
      }
      
      private function onPlayerControlEvent(param1:PlayerControlEvent) : void {
         var _loc2_:* = false;
         var _loc3_:* = NaN;
         var _loc4_:* = NaN;
         switch(param1.type)
         {
            case PlayerControlEvent.PLAYER_SIZE_CHANGED:
            case PlayerControlEvent.FULLSCREEN_CHANGED:
               _loc2_ = param1.data.fullscreen;
               _loc3_ = param1.data.width;
               _loc4_ = param1.data.height;
               this.topbar.setSize(_loc3_,_loc4_);
               if(_loc2_)
               {
                  this._mainMc.addChild(this.topbar);
                  this.topbar.x = this.topbar.y = 0;
               }
               else if(this._mainMc.contains(this.topbar))
               {
                  this._mainMc.removeChild(this.topbar);
               }
               
               break;
         }
      }
      
      private function tweenShow() : void {
         Tweener.killTweensOf(this.topbar);
         this.topbar.y = -this.topbar.height;
         Tweener.to(this.topbar,0.5,{"y":0});
      }
      
      private function tweenHide() : void {
         Tweener.killTweensOf(this.topbar);
         this.topbar.y = 0;
         Tweener.to(this.topbar,0.5,{"y":-this.topbar.height});
      }
      
      private function onMetadata(param1:ConductorEvent) : void {
         var _loc2_:String = this._playerProxy.playerData.videoTitle;
         this.topbar.title = _loc2_;
      }
      
      private function onStateChanged(param1:ConductorEvent) : void {
         var _loc2_:String = param1.data as String;
         this.topbar.enableSizeDefer = true;
         switch(_loc2_)
         {
            case ConductorState.VIDEO_END:
               this.topbar.enableSizeDefer = false;
               break;
            case ConductorState.VIDEO:
               this.topbar.visible = true;
               break;
         }
      }
   }
}
